<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
session_start();
//
$user_name = "root";
$pass_word = "haddons";
$database = "PPI";
$server = "127.0.0.1";
//
$db_handle = mysql_connect($server, $user_name, $pass_word);
$db_found = mysql_select_db($database, $db_handle);
//    
include 'i_functions.php';

function CrossCheck($Columns) {
    $Data = $_SESSION['Data'];
    $ColumnCount = count($Columns);

    $lnct = count($Data);
    for ($ln = 0; $ln < $lnct; $ln++) {
        $xCheckData = "";
        $Err = "";
        $ClaimID = "";
        $ClientID = "";
        $ClientName = "";
        $LenderName = "";
        $Claim = array();
        $Client = array();
        $Line = $Data[$ln];
        $SourceID = $Line[0];
        if ($SourceID) {
            //$xCheckData .= $SourceID;
            $SQL = "SELECT * FROM claims WHERE SourceID = '" . $SourceID . "'";
            $result = mysql_query($SQL);
            $num_rows = mysql_num_rows($result);
            if ($result) {
                if ($num_rows > 0) {
                    $Claim = mysql_fetch_assoc($result);
                    $ClaimID = $Claim['ID'];

                    $ClientID = $Claim['ClientID'];
                    $SQL2 = "SELECT Surname FROM client WHERE ID = '" . $ClientID . "'";
                    $result2 = mysql_query($SQL2);
                    $num_rows2 = mysql_num_rows($result2);
                    if ($result2) {
                        if ($num_rows2 > 0) {
                            $Client = mysql_fetch_assoc($result2);
                            $ClientName = $Client['Surname'];
                            if (strcasecmp(trim($ClientName), trim($Line[3])) !== 0) {
                                //$ClientName = "<span style='color:red; font-weight:bold;'>" . $ClientName . "</span>";
                                $Err .= "Client name mismatch. ";
                            }
                        }
                    }
                    $LenderName = $Claim['LenderName'];
                    if (strcasecmp(trim($LenderName), trim($Line[4])) !== 0) {
                        //$LenderName = "<span style='color:blue; font-weight:bold;'>" . $LenderName . "</span>";
                        $Err .= "Lender name mismatch. ";
                    }
                    //$xCheckData = "<span style='color:#aaaaaa;'>" . $ClaimID . " | " . $ClientID . " | " . $ClientName . " | " . $LenderName . "</span>";
                } else {
                    //$xCheckData = "<span style='color:black; font-weight:bold;'>No Claim with ID = " . $SourceID . "</span>";
                    $Err .= "No claim for this ID. ";
                }
            }
        } else {
            //$xCheckData = "<span style='color:black; font-weight:bold;'>No ID!</span>";
            $Err = "No ID! ";
        }
        $Data[$ln][9] = $ClaimID;
        $Data[$ln][10] = $ClientID;
        $Data[$ln][11] = $ClientName;
        $Data[$ln][12] = $LenderName;
        $Data[$ln][13] = $Err;
        /* $len = strlen($Data[$ln][8]);
          $LastChar = substr($Data[$ln][8], $len - 1, 1);
          $Data[$ln][14] = ord($LastChar);
         */
    }
    $_SESSION['Data'] = $Data;
    return true;
}

function Update($TableName) {
    if (strpos($TableName, ".csv")) {
        $dlim = ",";
    } else {
        $dlim = "\t";
    }
    $Data = $_SESSION['Data'];
    $Trace = $_SESSION['Trace'];
    $lnct = count($Data);
    //print "<br> lnct = " . $lnct . "<br>EOL = " . ord(PHP_EOL) . "<br>";
    $file_handle = fopen($TableName, "w");
    $Line = $_SESSION['Row1'];
    $len = strlen($Line);
    $LastChar = substr($Line, $len - 1, 1);
    if ($LastChar <> PHP_EOL) {
        $Line = $Line . PHP_EOL;
    }
    fwrite($file_handle, $Line);
    for ($ln = 0; $ln < $lnct; $ln++) {
        $Line = implode($dlim, $Data[$ln]);
        $len = strlen($Line);
        $LastChar = substr($Line, $len - 1, 1);
        if ($LastChar <> PHP_EOL) {
            $Line = $Line . PHP_EOL;
        }
        if ($Trace) {
            $NewLen = strlen($Line);
            $NewLastChar = substr($Line, $NewLen - 1, 1);
            print "<br>Line = " . $Line;
            print ", length=" . $len . ", LastChar=" . $LastChar . " (" . ord($LastChar) . "), NewLen=" . $NewLen . ", LastChar=" . $NewLastChar . "(" . ord($NewLastChar) . ")";
        }
        fwrite($file_handle, $Line);
    }
    fclose($file_handle);
    return true;
}

/*
 * ******************************************************************************
 *           M A I N   L O G I C
 * ******************************************************************************
 */

$_SESSION['Trace'] = "";
$DataFile = "CaseLoadSpreadsheet20Nov2013.txt";
$DataPath = "data/" . $DataFile;
$Columns = array("ID", "Title", "First Name", "Surname", "Lender", "Action Taken", "Date Worked On", "Comments", "Current Stage", "ClaimID", "ClientID", "ClientName", "Lender", "Error", "EOL");
$Options = array();
$_SESSION['DataType'] = "txt";
$Options['SkipRow1'] = "Y";
$Result = BuildDataArray($DataPath, $FormID, $Columns, $Options);
$_SESSION['Work'] = array();
//
$RetVal = CrossCheck($Columns);
//
//var_dump($_SESSION['Data']);
$RetVal2 = Update($DataPath);
//
$HTML = Array2Table($xData, $Columns, $Options);
include 'i_head.php';
print $HTML;
include 'i_foot.html';
?>
